package com.gz.moreaseManager.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.core.exception.BizExceptionEnum;
import com.gz.moreaseManager.core.exception.ServiceException;
import com.gz.moreaseManager.sys.entity.Dict;
import com.gz.moreaseManager.sys.mapper.DictMapper;
import com.gz.moreaseManager.sys.service.IDictService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.sys.transfer.SysDictItem;
import com.gz.moreaseManager.sys.transfer.SysDictVo;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-21
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Resource
    private DictMapper dictMapper;
    /**
     * 根据ID获取所有子类目字典
     * @param id
     * @return
     */
    public List<Dict> getChildDict(Integer id ){
        Wrapper wrapper = new EntityWrapper<Dict>();
        wrapper.eq("pid",id);
        List<Dict> list = dictMapper.selectList(wrapper);
        if( UtilTool.isNotBlank( list ))
            return list ;
        return null;
    }

    /**
     * 批量新增字典项目
     * @param itemList
     * @param pid
     */
    public void addSysDictItem(List<SysDictItem> itemList, Integer pid ) {
        if(UtilTool.isNotBlank( itemList )){
            for( SysDictItem dictItem : itemList ){
                Dict item = new Dict();
                item.setCode( dictItem.getCodeItem());
                item.setName( dictItem.getNameItem());
                item.setNum( dictItem.getNumItem());
                item.setPid( pid );

                dictMapper.insert(item);
            }
        }
    }

    @Override
    public Dict getSysDictInfo(Integer id) {
        if( UtilTool.isBlank( id ))
            return null;
        return dictMapper.selectById(id);
    }

    @Override
    public Dict getSysDictInfo(String code) {
        if( UtilTool.isBlank( code ))
            return null;
        Dict queryDict = new Dict();
        queryDict.setCode( code );
        return dictMapper.selectOne(queryDict);
    }

    @Override
    @Transactional
    public Boolean addSysDict(SysDictVo sysDictVo) {

        //1.判断有没有该字典
        Dict sysDict = getSysDictInfo(sysDictVo.getCode());
        if (UtilTool.isNotBlank(sysDict)) {
            throw new ServiceException(BizExceptionEnum.DICT_EXISTED);
        }

        //添加字典
        Dict dict = new Dict();
        dict.setCode(sysDictVo.getCode());
        dict.setName(sysDictVo.getName());
        dict.setNum(0);
        dict.setPid(0);//字典类别为主类目
        dict.setTips(sysDictVo.getTips());

        dictMapper.insert(dict);
        //添加字典条目
        if (UtilTool.isNotBlank(sysDictVo.getItemList())) {
            addSysDictItem(sysDictVo.getItemList(),dict.getId());
        }

        return true;
    }

    @Override
    @Transactional
    public Boolean updateSysDict(SysDictVo sysDictVo) {

        //1.修改字典名称
        Dict sysDict = new Dict();
        sysDict.setId( sysDictVo.getId() );
        sysDict.setName( sysDictVo.getName() );
        dictMapper.updateById(sysDict);
        //删除字典条目
        Wrapper wrapper = new EntityWrapper<Dict>();
        wrapper.eq("pid",sysDictVo.getId());
        dictMapper.delete(wrapper);
        //添加字典条目
        if (UtilTool.isNotBlank(sysDictVo.getItemList())) {
            addSysDictItem(sysDictVo.getItemList(),sysDictVo.getId());
        }
        return true;
    }

    @Override
    public SysDictVo findDictInfoById(Integer id) {
        SysDictVo sysDictVo = new SysDictVo();
        //1.获取字典项
        Dict sysDict = getSysDictInfo( id );
        return transferData(sysDict,sysDictVo);
    }

    @Override
    public SysDictVo findDictInfoByCode(String code) {
        SysDictVo sysDictVo = new SysDictVo();

        //1.获取字典项
        Dict sysDict = getSysDictInfo( code );
        return transferData(sysDict,sysDictVo);
    }

    @Override
    public Map<String, String> findMapToCodeByCode(String code) {
        Map<String, String> dictMap;
        if(UtilTool.isNotBlank( code )){
            dictMap = new HashMap<>();
            List<Dict> list = findChildDict(code);
            if( UtilTool.isNotBlank( list )){
                for(Dict dict : list ){
                    dictMap.put(dict.getCode(),dict.getName());
                }
            }
            return dictMap ;
        }
        return null;
    }

    @Override
    public Map<String, String> findMapToNameByCode(String code) {
        Map<String, String> dictMap;
        if(UtilTool.isNotBlank( code )){
            dictMap = new HashMap<>();
            List<Dict> list = findChildDict(code);
            if( UtilTool.isNotBlank( list )){
                for(Dict dict : list ){
                    dictMap.put(dict.getName(),dict.getCode());
                }
            }
            return dictMap ;
        }
        return null;
    }

    /**
     * 根据code 返回子字典项
     * @param code
     * @return
     */
    public List<Dict> findChildDict( String code ){
        if( UtilTool.isNotBlank( code )){
            //1.获取字典项
            Dict sysDict = getSysDictInfo( code );
            //2.获取子类目
            if( UtilTool.isNotBlank( sysDict )){
                List<Dict> list = getChildDict( sysDict.getId());
                return list;
            }
        }

        return null;
    }

    /**
     * 转换数据
     * @return
     */
    private SysDictVo transferData(Dict sysDict,SysDictVo sysDictVo){
        if( UtilTool.isNotBlank( sysDict ))
            BeanUtils.copyProperties(sysDict,sysDictVo);

        //2.获取子类目
        List<Dict> list = getChildDict( sysDict.getId());
        List<SysDictItem> items = new ArrayList<>();
        if( UtilTool.isNotBlank( list )){
            for(Dict dict : list ){
                SysDictItem sysDictItem = new SysDictItem();
                sysDictItem.setItemId( dict.getId() );
                sysDictItem.setCodeItem( dict.getCode() );
                sysDictItem.setNameItem( dict.getName() );
                sysDictItem.setNumItem( dict.getNum() );

                items.add(sysDictItem);
            }
        }

        sysDictVo.setItemList( items );

        return sysDictVo;
    }

}
